package structure.myHashBuck;

import java.util.HashSet;

public class HashBuck {

    //静态内部类
    static class Node{
        public int key;
        public int val;
        public Node next;

        public Node(int key,int val){
            this.key = key;
            this.val = val;
        }
    }

    public Node[] array;
    public int usedSize;
    public static final double DEFAULT_LOAD_FACTOR = 0.75;

    public HashBuck(){
        this.array = new Node[10];
    }

    /**
     * put函数
     * @param key 键
     * @param val 值
     */
    public void put(int key,int val){
        //1.找到key所在的位置
        int index = key % this.array.length;

        //2.遍历这个下标的链表,看是不是有相同的key,有就更新val
        Node cur = array[index];
        while (cur != null){
            if (cur.key == key){
                cur.val = val;
                return;
            }
            cur = cur.next;
        }

        //3.没有这个key这个元素(头插法)
        Node node = new Node(key,val);
        node.next = array[index];
        array[index] = node;
        this.usedSize++;

        //4.插入元素成功之后,检查当前散列表的负载因子.
        if (loadFactor() >= DEFAULT_LOAD_FACTOR){
            resize();//扩容
        }

    }

    /**
     * 重新哈希
     */
    private void resize(){
        Node[] newArray = new Node[array.length*2];
        for (int i = 0; i < array.length; i++) {
            Node cur = array[i];
            while (cur != null){
                int index = cur.key % newArray.length;//获取新的下标
                //把cur这个节点,以头插/尾插的形式 插入到新的数组对应下标的链表中
                //如果是尾插法,需要再写一个循环来找尾巴,这里用的是头插法.
                Node curNext = cur.next;
                cur.next = newArray[index]; //先绑定后面
                newArray[index] = cur; //绑定前面
                cur = curNext;
            }
        }
        array = newArray; //这样之前的数组就没人引用了,就会被JVM回收了.
    }

    private double loadFactor(){
        return 1.0*usedSize/array.length;
    }

    /**
     * 根据key获取value值
     * @param key 键
     * @return value
     */
    public int get(int key){
        //1.找到key所在的位置
        int index = key % this.array.length;
        //2.遍历这个下标的链表
        Node cur = array[index];
        while (cur != null){
            if (cur.key == key){
                return cur.val;
            }
            cur = cur.next;
        }
        return -1;
    }

    public static void main(String[] args) {
        HashBuck hashBuck = new HashBuck();
        hashBuck.put(1,1);
        hashBuck.put(12,12);
        hashBuck.put(3,3);
        hashBuck.put(6,6);
        hashBuck.put(7,7);
        hashBuck.put(2,2);
        hashBuck.put(11,11);
        //hashBuck.put(8,8);
        System.out.println(hashBuck.get(11));
    }



}
